﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using DevM.Authorization;
using DevM.Controllers;
using DevM.Users;
using DevM.Web.Models.Users;
using DevM.Users.Dto;
using DevM.Development.Projects.Dto;
using DevM.Development.ReleaseRecords;
using DevM.Development.ReleaseRecords.Dto;
using DevM.Web.Models.ReleaseRecord;
using DevM.Development.Projects;
using System.Linq;

namespace DevM.Web.Controllers
{
    [AbpMvcAuthorize(PermissionNames.Pages_Projects)]
    public class ReleaseRecordController : DevMControllerBase
    {
        private readonly IReleaseRecordAppService releaseRecordAppService;
        private readonly IUserAppService userAppService;
        private readonly IProjectAppService projectAppService;

        public ReleaseRecordController(IReleaseRecordAppService releaseRecordAppService, IUserAppService userAppService, IProjectAppService projectAppService)
        {
            this.releaseRecordAppService = releaseRecordAppService;
            this.userAppService = userAppService;
            this.projectAppService = projectAppService;
        }

        public async Task<ActionResult> Index(GetReleaseRecordInput input)
        {
            var releaseRecords = await releaseRecordAppService.GetReleaseRecordList(input);
            var project = await projectAppService.GetProject(new EntityDto<long>(input.ProjectId));
            var environments = await projectAppService.GetEnvironmentList(new EntityDto<long>(input.ProjectId));
            var model = new ReleaseRecordListViewModel
            {
                ReleaseRecords = releaseRecords,
                Project = project,
                Environments = environments
            };
            return View(model);
        }

        public async Task<ActionResult> ReleaseLogs(GetReleaseLogInput input)
        {
            var project = await projectAppService.GetProject(new EntityDto<long>(input.ProjectId));
            var releaselogs = await releaseRecordAppService.GetReleaseLogList(input);
            var model = new ReleaseLogListViewModel
            {
                Project = project,
                ReleaseLogs = releaselogs
            };
            return View(model);
        }

        public async Task<ActionResult> Feedbacks(GetFeedbackListInput input)
        {
            var feedbacks = await releaseRecordAppService.GetFeedbackList(input);
            var releaseLog = input.ReleaseLogId > 0 ? await releaseRecordAppService.GetReleaseLog(new EntityDto<long>(input
                .ReleaseLogId)) : await releaseRecordAppService.GetReleaseLog(new EntityDto<long>(input.ReleaseRecordId), new EntityDto<long>(input.EnvironmentId));
            var project = await projectAppService.GetProject(new EntityDto<long>(releaseLog.ReleaseRecord.ProjectId));
            var model = new FeedbackListViewModel
            {
                Feedbacks = feedbacks,
                ReleaseLog = releaseLog,
                Project = project,
                ReleaseLogId = input.ReleaseLogId
            };
            return View(model);
        }

        public async Task<ActionResult> EditFeedbackModal(long feedbackId)
        {
            var feedback = await releaseRecordAppService.GetFeedback(new EntityDto<long>(feedbackId));
            var model = new EditFeedbackViewModel
            {
                Feedback = feedback
            };
            return View("_EditFeedbackModal", model);
        }

        public async Task<ActionResult> EditReleaseLogModal(long releaseRecordId,long environmentId)
        {
            var log = await releaseRecordAppService.GetReleaseLog(new EntityDto<long>(releaseRecordId), new EntityDto<long>(environmentId));
            var model = new EditReleaseLogViewModel
            {
                ReleaseLog = log
            };
            return View("_EditReleaseLogModal", model);
        }
    }
}
